/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package exercises.day02.pt03;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

/**
 *  @author (jaice)
 * @version 0.1 (may 28, 2012)
 * 
 * a. Create a simple console-based Java application which will allow tellers of Earnest Bank to use a 
 * collection-based data source.  The application should have the following specifications:
 * 
i. A method which allows the tellers to store registration details (registration id, first name, 
 * last name, address, account type, annual income, and phone number) in a collection. 
 * 
 * Write this method passing a class as a parameter.  Overload this method to accept 
 * a comma-separated String as a parameter. 
 * 
 * The overloaded method should be able to tokenize the CSV and store it as part of the collection.  
 * Use the following as test: "9, Silas, Marner, Evergreen Terrace, Savings, 350000, 09885832";
 * 
ii. A method which will allow tellers to search by first name and return a list of result set;
 * 
iii. A method which will allow tellers to retrieve all registrations by annual income.  
 * Overload this method to accept the annual income and a boolean value to include incomes 
 * below or above the supplied annual income.

Code a main application which will test the methods.  Format the output as necessary.
 */
public class BankTransaction {

    private static ArrayList<BankCustomer> list = new ArrayList<BankCustomer>();

    public void addCustomer(String details) {


        BankCustomer bc = new BankCustomer(details);
        list.add(bc);

    }

    public void addCustomer(String id, String lname, String fname, String address, String type, double income, String phone) {


        BankCustomer bc = new BankCustomer(id, fname, lname, address, type, income, phone);
        list.add(bc);
    }

    public ArrayList< BankCustomer> searchName(String search) {
        ArrayList<BankCustomer> found = new ArrayList();

        for (BankCustomer bc : list) {

            if (bc.getFname().indexOf(search) > -1) {
                found.add(bc);

            }

        }

        return found;

    }

    /**
     * 
     * @return 
     */
    public ArrayList< BankCustomer> returnAll() {
        return list;
    }

    /**
     * 
     * @return 
     */
    public ArrayList< BankCustomer> returnByIncome() {

        ArrayList< BankCustomer> sorted = new ArrayList<BankCustomer>();

        sorted = this.list;

        Collections.sort(sorted, new Comparator<BankCustomer>() {

            public int compare(BankCustomer s1, BankCustomer s2) {
                return Double.toString(s1.getIncome()).compareToIgnoreCase(Double.toString(s2.getIncome()));

            }
        });

        return sorted;

    }

    public ArrayList< BankCustomer> returnByIncome(Double amount, boolean above) {



        ArrayList< BankCustomer> sorted = new ArrayList<BankCustomer>();


        for (BankCustomer bc : list) {
            if (above == true) {
                if (bc.getIncome() > amount) {
                    sorted.add(bc);
                }
            }
            if (above == false) {
                if (bc.getIncome() < amount) {
                    sorted.add(bc);
                }
            }

        }



        Collections.sort(sorted, new Comparator<BankCustomer>() {

            public int compare(BankCustomer s1, BankCustomer s2) {
                return Double.toString(s1.getIncome()).compareToIgnoreCase(Double.toString(s2.getIncome()));

            }
        });

        return sorted;

    }
}
